package org.wdj.jpa.controller;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.wdj.jpa.MyResponse;
import org.wdj.jpa.entity.SysUser;
import org.wdj.jpa.service.SysUserService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/api/user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    //新建记录
    @PostMapping(value = "/save")
    public MyResponse save(@RequestBody SysUser sysUser) {
        return sysUserService.save(sysUser);
    }

    //find by username, use json content type
    @PostMapping(value = "/find")
    public SysUser find(@RequestBody SysUser sysUser) {
        return sysUserService.find(sysUser.getUsername());
    }

    //update record
    @PostMapping(value = "/update")
    public MyResponse update(@RequestBody SysUser sysUser) {
        return sysUserService.update(sysUser);
    }

    //删除记录
    @DeleteMapping(value = "/delete")
    public MyResponse delete(@RequestBody SysUser sysUser) {
        return sysUserService.delete(sysUser.getUsername());
    }

    //add user list
    @PostMapping("/saveAll")
    public MyResponse saveAll(@RequestBody List<SysUser> sysUserList) {
        return sysUserService.saveAll(sysUserList);
    }

    //获取列表
    @GetMapping(value = "/list")
    public List<SysUser> list() {
        return sysUserService.list();
    }

    @GetMapping(value = "/page")
    public Page<SysUser> page(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        //page 从 0 开始编号, 默认浏览第一页，每页大小 3
        return sysUserService.page(page, size);
    }

    @GetMapping(value = "search")
    public Page<SysUser> search(@RequestParam String nickname, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        return sysUserService.searchByNickname(nickname, page, size);
    }
}
